<?php
/**
 * Created by PhpStorm.
 * User: 庆金
 * Date: 2019-12-13
 * Time: 23:06
 */
class AysMysql{
    public $dbSource = "";
    //mysql 配置
    public $dbConfig = [];
    public function __construct(){
        $this->dbSource = new Swoole\Coroutine\MySQL();
        $this->dbConfig = [
            'host'  =>  '127.0.0.1',
            'port'  =>  3306,
            'user'  =>  'lexunw',
            'password'  =>  'lexunw0113',
            'database'  =>  'swoole',
            'charset'   =>  'utf8',
        ];
    }

    public function add(){

    }

    public function update(){

    }

    /**
     * mysql 执行逻辑
     * @param $id
     * @param $username
     * @return bool
     */
    public function execute($id,$username){
        $this->dbSource->connect($this->dbConfig);
        $sql = "select * from test where id = 1";
        $result = $this->dbSource->query($sql);
        if ($result === false){

        }elseif ($result === true){

        }else{
            print_r($result);
        }

//        $this->dbSource->connect($this->dbConfig,function ($db,$result){
//            if ($result === false){
//                var_dump($db->connect_error);
//            }
//            $sql = "select * from test where id = 1";
//            // query (add select update delete)
//            $db->query($sql,function ($db,$result){
//                // select => result 返回查询结果集
//                // add update delete 返回booler
//                if ($result === false){
//
//                }elseif ($result === true){
//
//                }else{
//                    print_r($result);
//                }
//            });
//        });
//        return true;
    }
}
go(function(){
    $obj = new AysMysql();
    $obj->execute(1,'helloSwoole');
});
